package com.TextFormat;


import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;
import com.Data.Tag;
import com.Data.Tag.onSearchTagCompleted;


public class TagEditText extends MultiAutoCompleteTextView implements OnItemClickListener {

    private TagEditText me;

    private boolean shouldDownload = true;

    // TextWatcher, If user type any country name and press comma then following
    // code will regenerate chips
    private final TextWatcher textWather = new TextWatcher() {

        @Override
        public void afterTextChanged(Editable arg0) {

            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            if (count >= 1) {
                if (s.charAt(start) == separator)
                    UtilFonts.setChips(me);

                if (shouldDownload)
                    showTags(s.toString());
            }
        }
    };

    protected char separator = ',';

    // Constructor
    public TagEditText(Context context) {

        super(context);
        init(context);
        this.me = this;
    }

    // Constructor
    public TagEditText(Context context, AttributeSet attrs) {

        super(context, attrs);
        init(context);
        this.me = this;
    }

    // Constructor
    public TagEditText(Context context, AttributeSet attrs,
                int defStyle) {

        super(context, attrs, defStyle);
        init(context);
        this.me = this;
    }

    // set listeners for item click and text change
    public void init(Context context) {

        setOnItemClickListener(this);
        addTextChangedListener(textWather);
        this.me = this;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        UtilFonts.setChips(me);
        shouldDownload = true;
        // call generate chips when user select any item from auto
        // complete
    }

    private void showTags(String text) {

        String[] chips;
        String search = text;
        if (text.contains(",") && text.length() > 2) {
            chips = text.split(",");
            search = chips[chips.length - 1].trim();
        }
        Log.e("BUSCANDO", search);
        Tag tg = new Tag();
        tg.setName(search);
        tg.setCallbackSearch(new onSearchTagCompleted() {

            @SuppressWarnings("unchecked")
            @Override
            public void Completed(Tag[] result) {

                ArrayAdapter<String> adapter = (ArrayAdapter<String>) getAdapter();
                adapter.clear();
                for (Tag tg : result) {

                    adapter.add(tg.getName());

                }

            }
        });
        shouldDownload = false;
        tg.execute(Tag.ACTION_SEARCH);

    }
}
